#!/usr/bin/env python
# coding: UTF-8

import gtk
import gobject
from gtk import gdk

import pygtk

class GTKGraph(gtk.DrawingArea):
    '''
    a class for drawing a monitor graph
    '''
    def __init__(self):
        gtk.DrawingArea.__init__(self)
        self.connect ('expose_event', self.expose)
        self.data=None
        self.curent_dir=None

    def expose(self, widget, event):
        self.context = widget.window.cairo_create()
        # set a clip region for the expose event
        self.context.rectangle(event.area.x, event.area.y,
                               event.area.width, event.area.height)
        self.context.clip()
        self.width, self.height = event.area.width, event.area.height
        self.draw(self.context)

    def show(self, data, curent_dir):
        self.data=data
        self.curent_dir=curent_dir
        self.queue_draw()

    def clear(self):
        self.data=None
        self.curent_dir=None
        self.queue_draw()
        

    def draw(self, context):       
        rect = self.get_allocation()
        x = rect.width 
        y = rect.height
        self.width, self.height = x, y
        self.xoff, self.yoff = x*.05, y*.1        
        height = self.height; width=self.width
        xoff, yoff = self.xoff, self.yoff        
        
        # clear the backgorund with white
        context.set_source_rgb (1, 1, 1)
        context.set_line_width (0.5)
        context.move_to (0,0)
        context.line_to (x,0)
        context.line_to (x,y)
        context.line_to (0,y)
        context.line_to (0,0)
        context.fill_preserve()
        context.set_source_rgb(0, 0, 0)
        context.stroke()

        # plot axis
        # plot horizontal line
        context.set_source_rgb(0,0,0)
        context.set_line_width(1)
        context.move_to (xoff,       height-yoff*.75)
        context.line_to (width-xoff, height-yoff*.75)        
        context.stroke() 
        # plot vertical line
        context.set_source_rgb(0,0,0)
        context.set_line_width(1)
        context.move_to (xoff*.75,        yoff)
        context.line_to (xoff*.75, height-yoff)        
        context.stroke()
        # plot data in blue
        
        if not self.data: return
        prof_vals = self.data[0]
        prof_dirs = self.data[1]
        self.scale = (height-3*yoff)*1.0/(max(prof_vals) - min(prof_vals))        
        for i in range(1, len(prof_vals)):
            dr_prev, val_prev = prof_dirs[i-1], prof_vals[i-1]
            dr , val          = prof_dirs[i]  , prof_vals[i]  
            x_prev, y_prev = self.return_coords(dr_prev, val_prev)
            x, y           = self.return_coords(dr, val)
            context.set_source_rgb(0,0,1)
            context.set_line_width(.5)
            context.move_to (x_prev, y_prev)
            context.line_to (x,      y)        
            context.stroke()
        # plot curent direction in red
        x0, y0 = self.return_coords( self.curent_dir, 0)
        x1, y1 = self.return_coords( self.curent_dir, max(prof_vals))
        context.set_source_rgb(1,0,0)
        context.set_line_width(.5)
        context.move_to (x0, y0)
        context.line_to (x1, y1)        
        context.stroke()
   
    def return_coords(self, dr, y):
        width, height, scale = self.width, self.height, self.scale
        xoff, yoff = self.xoff,self.yoff
        return dr/360.0*(width-xoff*2)+xoff, height-yoff-y*scale


    def get_direction(self, x):
        direction =  (x-self.xoff)*1.0/(self.width-2*self.xoff) * 360
        self.curent_dir = direction
        self.queue_draw()
        return direction
        
